import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:xiongan_app_yy/pages/common/BaseMaterialInfo.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/pages/common/TextFieldItem.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/TextItemCard.dart';
import 'package:xiongan_app_yy/provider/automounting_provider.dart';
import 'package:xiongan_app_yy/utils/image_utils.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

class AutoMountingDDetail extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AutoMountingDDetail();
  }
}

class _AutoMountingDDetail extends State<AutoMountingDDetail> {
  Map<String, dynamic> map = Map();
  String nameStr = "";
  String image = "assets/images/build.png";

  void initCallBack(dynamic res, {String successMsg}) {
    //初始化上架/下架接口回调
    if (res['flag'] == "success") {
      TostUtil.showToast(successMsg);
      Provider.of<AutoMountingProvider>(context).getDetailListData(orderid: map["orderid"]);
      Navigator.pop(context);
    } else {
      TostUtil.showToast(res['msg']);
    }
  }

  void downCallBack(dynamic res) {
    //下架接口回调
    initCallBack(res, successMsg: "下架成功");
  }

  void upCallBack(dynamic res) {
    //上架接口回调
    initCallBack(res, successMsg: "上架成功");
  }

  TextEditingController code = TextEditingController();
  TextEditingController loc = TextEditingController();

  @override
  Widget build(BuildContext context) {
    map = Provider.of<AutoMountingProvider>(context).detailData;
    nameStr = Provider.of<AutoMountingProvider>(context).inPutawaytypename;
    print(nameStr);
    if (map["imageurl"] != null && image == "assets/images/build.png") {
      ImageDownloadUtil.getImageBase64(map["imageurl"], (im) {
        setState(() {
          image = im;
        });
      });
    }
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0xFF00A5F1),
        title: Text(
          "分配上架",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(
        bgColor:  Color(0x66BCBCBC),
        visibility: Provider.of<AutoMountingProvider>(context).netStatus4,
        builder: Column(
          children: <Widget>[
            Expanded(
              child: Scrollbar(
                child: SingleChildScrollView(
                  child: Column(
                    children: <Widget>[
                      Visibility(
                        visible: map.containsKey("worktoolcode"),
                        child: TextItemCard(
                          name: "上架托盘码",
                          value: map['worktoolcode'] ?? "暂无",
                        ),
                      ),
                      TextItemCard(
                        name: "上架库区",
                        value: map['areaname'] ?? "暂无",
                      ),
                      TextItemCard(
                        name: "上架货架",
                        value: map['shelfname'] ?? "暂无",
                      ),
                      TextItemCard(
                        name: "上架货位",
                        value: map['tolocationcode'] ?? "暂无",
                      ),
                      Column(
                        children: <Widget>[
                          BaseMaterialInfo(
                            imgwid: ImageDownloadUtil.isImageType(image),
                            options: {
                              "materialname": "物资名称",
                              "materialcode": "实物ID",
                              "baseunitname": "基本单位",
                              "planquantity": "计划上架数"
                            },
                            data: map,
                          ),
                          Container(
                            padding: EdgeInsets.all(15),
                            child: Column(
                              children: <Widget>[
                                TextFieldItem(
                                  scan: false,
                                  hintText: "输入实际上架数",
                                  onChangedFuns: (val) {
                                    map['quantity'] = val;
                                  },
                                ),
                                Visibility(
                                  visible: nameStr != "自动行吊上架" &&nameStr != "人工行吊上架" &&nameStr != "合并上架"&&
                                      map['devicetypeid'] == '1',
                                  child: TextFieldItem(
                                    hintText: "扫描或输入托盘码",
                                    textEditingController: code,
                                    onChangedFuns: (val) {
                                      map['worktoolcode'] = val;
                                    },
                                  ),
                                ),
                                Visibility(
                                  visible: (nameStr != "自动行吊上架" ||
                                          nameStr != "人工行吊上架") &&
                                      map['devicetypeid'] == '1',
                                  child: Container(
                                    child: Row(
                                      children: <Widget>[
                                        Expanded(
                                          child: TextFieldItem(
                                            hintText: "扫描或输入托盘存放位置",
                                            textEditingController: loc,
                                            onChangedFuns: (val) {
                                              map['fromlocationcode'] = val;
                                            },
                                          ),
                                        ),
                                        Visibility(
                                          visible: !map['issendputdowntask'],
                                          child: Container(
                                            margin: EdgeInsets.only(left: 10),
                                            child: FlatButton(
                                              color: Color(0xFF42AFF0),
                                              padding: EdgeInsets.only(
                                                  top: 13,
                                                  bottom: 13,
                                                  left: 10,
                                                  right: 10),
                                              child: Text(
                                                "发送指令下架",
                                                style: TextStyle(
                                                    fontSize: 12,
                                                    color: Colors.white),
                                              ),
                                              onPressed: () {
                                                Map<String, dynamic> sendObj =
                                                    Map();
                                                sendObj['fromlocationcode'] =
                                                    map['fromlocationcode'];
                                                sendObj['palletid'] =
                                                    map['palletid'];
                                                sendObj['palletcode'] =
                                                    map['palletcode'];
                                                sendObj['materialid'] =
                                                    map['materialid'];
                                                Provider.of<AutoMountingProvider>(
                                                        context)
                                                    .sendOrderTakeMaterialTask(
                                                        sendObj: sendObj,
                                                        callbackFuns:
                                                            downCallBack);
                                              },
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                )
                              ],
                            ),
                          )
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ),
            Row(
              children: <Widget>[
                Visibility(
                  visible: !map['issendputawaytask'],
                  child: Expanded(
                    child: FlatButton(
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(0))),
                      padding: EdgeInsets.all(15),
                      color: Color(0xFF42AFF0),
                      child: Text(
                        "发送上架指令",
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        Map<String, dynamic> sendObj = Map();
                        print(map);
                        sendObj['fromlocationcode'] = map['fromlocationcode'];
                        sendObj['palletid'] = map['palletid'];
                        sendObj['palletcode'] = map['palletcode'];
                        sendObj['materialid'] = map['materialid'];
                        sendObj['orderdispatchdetailid'] = map['orderdispatchdetailid'];
                        sendObj['quantity'] = map['quantity'];
                        sendObj['worktoolcode'] = map['worktoolcode'];
                        sendObj['tolocationid'] = map['tolocationid'];
                        sendObj['tolocationcode'] = map['tolocationcode'];
                        sendObj['planquantity'] = map['planquantity'];
                        sendObj['orderid'] = map['orderid'];
                        sendObj['orderdetailid'] = map['orderdetailid'];
                        sendObj['ordercheckid'] = map['ordercheckid'];
                        sendObj['fromlocationid'] = map['fromlocationid'];
//                      sendObj['ismodifylocation']=map['ismodifylocation']; //是否是人工选择货位
                        Provider.of<AutoMountingProvider>(context)
                            .sendOrderAutoPutqwayTask(
                                sendObj: sendObj, callbackFuns: upCallBack);
                      },
                    ),
                  ),
                ),
                Visibility(
                  visible: !(nameStr == "自动行吊上架" || nameStr == "人工行吊上架") &&
                      !map['isputaway'],
                  child: Expanded(
                    child: FlatButton(
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(0))),
                      padding: EdgeInsets.all(15),
                      color: Color(0xffda732a),
                      child: Text(
                        "选择货位",
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        print(map);
                        Navigator.of(context)
                            .pushNamed("/select_location", arguments: map);
                      },
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
